package com.qf.ability.resources.controller;

import com.alibaba.fastjson.JSONObject;
import com.qf.data.base.r.R;
import com.qf.data.base.r.Rs;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.UUID;

/**
 * description:
 * author: Ken
 * 公众号：Java架构栈
 */
@RestController
@RequestMapping("/res/img")
public class ImgResourcesController {

    @Value("${uploader.img}")
    private String imgUploaderPath;

//    @RequestMapping("/test")
//    public void test(@RequestBody Param body){
//        System.out.println("触发了！！！" + body);
//    }
//
//    @Data
//    public static class Param{
//        private String name;
//        private Integer age;
//    }


    /**
     * vue端 - 富文本编辑器的图片上传
     */
    @RequestMapping("/wangeditor/uploader")
    public String wangEditorImgUploader(MultipartFile file){
        File filePath = new File(imgUploaderPath);
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        //创建文件名称
        String fileName = UUID.randomUUID().toString();

        //上传
        try (
                InputStream in = file.getInputStream();
                OutputStream out = new FileOutputStream(new File(filePath, fileName));
        ){
            IOUtils.copy(in, out);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //返回的数据
        /*
        {
            "errno": 0, // 注意：值是数字，不能是字符串
            "data": {
                "url": "xxx", // 图片 src ，必须
                "alt": "yyy", // 图片描述文字，非必须
                "href": "zzz" // 图片的链接，非必须
            }
        }
         */

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("errno", 0);
        JSONObject sJsonObject = new JSONObject();
        sJsonObject.put("url", "http://localhost/res/img/get?fileName=" + fileName);
        jsonObject.put("data", sJsonObject);

        return jsonObject.toJSONString();
    }

    /**
     * 普通的图片上传
     * @param file
     * @return
     */
    @RequestMapping("/uploader")
    public R uploader(MultipartFile file){
        File filePath = new File(imgUploaderPath);
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        //创建文件名称
        String fileName = UUID.randomUUID().toString();

        //上传
        try (
                InputStream in = file.getInputStream();
                OutputStream out = new FileOutputStream(new File(filePath, fileName));
        ){
            IOUtils.copy(in, out);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return Rs.create(fileName);
    }

    /**
     * 根据图片的名称访问图片
     */
    @RequestMapping("/get")
    public void getImg(String fileName, HttpServletResponse response){
        try (
                FileInputStream in = new FileInputStream(new File(imgUploaderPath, fileName));
                ServletOutputStream out = response.getOutputStream();
        ) {

            byte[] buffer = new byte[1024];
            int len = -1;
            while((len = in.read(buffer)) != -1){
                out.write(buffer, 0, len);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
